/**
 * request处理基础类
 */
const app = getApp();
var _baseUrl = "https://www.raintec.cn";
// var _baseUrl = "http://192.168.1.201:8080";

 var headerGet = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "token": app.globalData.token
};
var headerPost = {
  "Content-Type": "application/x-www-form-urlencoded"
};
var headerPut = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "token": app.globalData.token
};
var headerDelete = {
  "Accept": "application/json",
  "Content-Type": "application/json",
  "token": app.globalData.token
};

/**
 * 设置统一的异常处理
 */
function setErrorHandler(handler) {
  _errorHandler = handler;
}

/**
 * 设置统一的异常处理
 */
function logoutHandler() {
  
}

  /**
   * GET类型的网络请求
   */
function  getRequest(url, success) {
    return requestAll(url, {}, headerGet, 'GET',success)
  }

  /**
   * DELETE类型的网络请求
   */
function deleteRequest(url,  success) {
  return requestAll(url, {}, headerDelete, 'DELETE', success)
  }

  /**
   * PUT类型的网络请求
   */
function  putRequest(url, data,success) {
  return requestAll(url, data, headerPut, 'PUT', success)
  }

  /**
   * POST类型的网络请求
   */
function postRequest(url, data, success) {
  return requestAll(url, data, headerPost, 'POST', success)
  }

  /**
   * 网络请求
   */
function requestAll(url, data, header, method, success) {
  if (url == 'https://www.raintec.cn/device/listByRegion') {
    // console.log(url, '我是url')
    // wx.showLoading({
    //   // mask: true,
    //   title:'实时数据加载中...'

    // })
  }else {
    wx.showLoading({
      mask: true,

    })
  }
 
    if(data==undefined){
      data={};
    }
    if(header!=null){
      header.token = app.globalData.token;
    }
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        data: data,
        header: header,
        method: method,
        complete: function (res) {
          wx.hideLoading();
         var data= res.data;
          if (res == null || res.data == null) {
            // console.error('网络请求失败');
            return;
          }
          if (res.data.status == 5) {
            wx: wx.showToast({
              title: res.data.msg
            });
            wx.removeStorage({
              key: 'user_info',
              success(res) {
                console.log(res)
              }
            })
            wx.redirectTo({ url: "../login/login" })
            return;
          }
          if (res.data.status == 11) {
            wx: wx.showToast({
              title: res.data.msg
            });
            wx.removeStorage({
              key: 'user_info',
              success(res) {
                console.log(res)
              }
            })
            wx.redirectTo({ url: "../login/login" })
            return;
          }
          if (res.data.status != 0) {
           
            wx: wx.showToast({
              title: res.data.msg
            });
           
          }

          success(res);
         
        }
        
      })
    })
  }






module.exports = {

  // 登录
  login: function (username, password,success) {
    
    let data = { username: username, password: password };
    return postRequest(_baseUrl + '/user/login', data, success).then(res => res.data);
  },
//根据用户id请求站点
  listStationByUid:function(success){
    return getRequest(_baseUrl + "/station/listByUid/" + app.globalData.userInfo.id, success).then(res => res.data);
  },
  //请求站点详情
  requestStationDetail: function ( id,success) {
    return getRequest(_baseUrl + "/station/listDetail/" + id, success).then(res => res.data);
  },
  listGateWayByUid: function (success) {
    console.log(app.globalData.userInfo)
    return getRequest(_baseUrl + "/gateWay/listByUId/" + app.globalData.userInfo.id, success).then(res => res.data);
  },

  //根据网关序列号查找摄像头
  listCameraByGserial: function (serial,success){
    return getRequest(_baseUrl + "/cameraG/listByGSerial/" + serial, success).then(res => res.data);
  },
  //根据网关序列号查找采集器
  listCollectorByGserial: function (serial, success) {
    return getRequest(_baseUrl + "/collector/listByGSerial/" + serial, success).then(res => res.data);
  }  ,
  //根据网关序列号查找设备区域
  listRegionByGserial: function (serial, success) {
    return getRequest(_baseUrl + "/regionInfo/list/" + serial, success).then(res => res.data);
  },
  //查找气象站的实时数据
  requestCSRealtime: function (serial, success) {
    return getRequest(_baseUrl + "/data/realtimeSingle/" + serial, success).then(res => res.data);
  },
  //网关下采集器的实时数据
  requestRealtime: function (gSerial, collectorAddr, success) {
    return postRequest(_baseUrl + "/data/realtime", { gSerial: gSerial, collectorAddr: collectorAddr}, success).then(res => res.data);
  },
  //根据阈值类型切换设备控制方式
  switchCtrlTypeByGroup: function (gSerial, deviceRegion, groupType, controlType , success){
    return postRequest(_baseUrl + "/device/switchCtrlTypeByGroup", { gSerial: gSerial, deviceRegion: deviceRegion, groupType: groupType, controlType: controlType }, success).then(res => res.data);
  },
  //查找区域性设备列表
  listDeviceByRegion: function (gSerial, deviceRegion, success) {
    return postRequest(_baseUrl + "/device/listByRegion", { gSerial: gSerial, deviceRegion: deviceRegion }, success).then(res => res.data);
  },
  //根据设备id查找定时
  listTimingsByDid: function (dId, success) {
    return getRequest(_baseUrl + "/timing/list/" + dId, success).then(res => res.data);
  },
  //根据设备id删除定时
  deleteTiming: function (id, success) {
    return deleteRequest(_baseUrl + "/timing/delete/" + id, success).then(res => res.data);
  },
  //添加定时
  addTiming: function (data, success) {
    return putRequest(_baseUrl + "/timing/add", data, success).then(res => res.data);
  },
  //修改定时
  updateTiming: function (data, success) {
    return putRequest(_baseUrl + "/timing/update", data, success).then(res => res.data);
  },
  //单路设备控制
  singleCtrl: function (gSerial, controllerAddr, deviceAddr, isDoubleSwitch, orders,success){
    // {
    //   gSerial: _this.data.device.gSerial,
    //     controllerAddr: _this.data.device.controllerAddr,
    //       deviceAddr: _this.data.device.deviceAddr,
    //         isDoubleSwitch: _this.data.device.isDoubleSwitch,
    //           orders: event.detail.value
    // }
    return postRequest(_baseUrl + "/device/ctrl", { gSerial: gSerial, controllerAddr: controllerAddr, deviceAddr: deviceAddr, isDoubleSwitch: isDoubleSwitch, orders: orders }, success).then(res => res.data);
  },
  //双路设备控制
  doubleCtrl: function (gSerial, controllerAddr, deviceAddr, isDoubleSwitch, orders, success) {
   
    return postRequest(_baseUrl + "/device/ctrl", { gSerial: gSerial, controllerAddr: controllerAddr, deviceAddr: deviceAddr, isDoubleSwitch: isDoubleSwitch, orders: orders }, success).then(res => res.data);
  },
  //POST /threshold/listByRegion  根据区域号获取阈值
  listThresByRegion: function (gSerial, deviceRegion, success) {

    return postRequest(_baseUrl + "/threshold/listByRegion", { gSerial: gSerial, deviceRegion: deviceRegion }, success).then(res => res.data);
  },
  //添加阈值
  addThreshold: function (data,success){
    return putRequest(_baseUrl + "/threshold/add", data, success).then(res => res.data);
  }
  ,
  //POST /threshold/listByRegion  根据区域号获取阈值
  listAlarmByRegion: function (gSerial, deviceRegion, success) {

    return postRequest(_baseUrl + "/alermOrder/list", { gSerial: gSerial, deviceRegion: deviceRegion }, success).then(res => res.data);
  },

  //添加报警命令
  addAlarm: function (data, success) {
    return putRequest(_baseUrl + "/alermOrder/add", data, success).then(res => res.data);
  }


}
